import java.util.*;

import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.*;
import utilities.event.*;
import utilities.utilities_components.*;

public class ReservationFormController
extends Window{
	
	//0 for active and room reserved
	//1 for active and not enough room reserved
	//2 for out-of-date but in history
	private String[] colors={"#00EC00","#FF5809","#8E8E8E"};
	private int currentFilterOp=0;
	
	private Textbox id;
	private Datebox timeIn;
	private Datebox timeOut;
	private Combobox type;
	private Intbox roomNr;
	private Checkbox partTime;
	private Textbox note;
	private Textbox name;
	private Longbox tel;
	private Combobox sex;
	private Combobox docType;
	private Textbox docNumber;
	private Button add,del;
	private Div listPane;
	private PlainList inner;
	private Label roomNote;
	
	public void initialize(){
		id=this.getFellow("INFO").getFellow("ID");
		timeIn=this.getFellow("INFO").getFellow("TIMEIN");
		timeOut=this.getFellow("INFO").getFellow("TIMEOUT");
		type=this.getFellow("INFO").getFellow("TYPE");
		roomNr=this.getFellow("INFO").getFellow("ROOMNR");
		partTime=this.getFellow("INFO").getFellow("PART_TIME");
		note=this.getFellow("INFO").getFellow("NOTE");
		name=this.getFellow("CLIENT_INFO").getFellow("NAME");
		tel=this.getFellow("CLIENT_INFO").getFellow("TELNUMBER");
		sex=this.getFellow("CLIENT_INFO").getFellow("SEX");
		docType=this.getFellow("CLIENT_INFO").getFellow("DOCTYPE");
		docNumber=this.getFellow("CLIENT_INFO").getFellow("DOCNUMBER");
		add=this.getFellow("BUTTON_PANE").getFellow("ADD");
		del=this.getFellow("BUTTON_PANE").getFellow("DEL");
		listPane=this.getFellow("LIST");
		roomNote=this.getFellow("SHOW_ROOMS");
	
		//alert(id.toString()+" "+timeIn.toString()+" "+timeOut.toString()+" "+type.toString()+" "+roomNr.toString()+" "+
		//	  partTime.toString()+" "+note.toString()+" "+name.toString()+" "+tel.toString()+" "+sex.toString()+" "+
		//	  docType.toString()+" "+docNumber.toString()+" "+add.toString()+" "+del.toString());
		
		inner=new PlainList();
		String[] head={"ID","顾客姓名","性别","证件类型","证件号码","电话","预计入住时间","预计离开时间","房型","房间数量","钟房"};
		int[] disabledCols={11,12};
		inner.setParent(listPane);
		inner.setHead(head);
		inner.setDisabled(disabledCols);
		inner.setColorIndexer(this.colors,12);
		inner.setListener(new PlainList.SelectListener(){
			public void onFocus(Component comp){
				if(inner.getSelectedIndex() >= 0){
					setPara(inner.getColDate(inner.getSelectedIndex()));
					String str=((Listcell)inner.getColDate(inner.getSelectedIndex()).get(11)).getLabel();
					roomNote.setValue("预订房间号: "+str);
					del.setDisabled(false);
				}else{
					roomNote.setValue("");
				}
			}
		});
		inner.setUpdateListener(new PlainList.UpdateListener(){
			public boolean onUpdate(List data){
				if(data.size() > 12 && data.get(12) instanceof Integer){
					Integer switcher=(Integer)data.get(12);
					if(!(switcher == 2 && currentFilterOp == 1) && 
					   !(switcher == 1 && currentFilterOp == 3) &&
					   !(switcher == 0 && currentFilterOp == 2) &&
					   currentFilterOp != 0)
						return false;
				}
				return true;
			}
		});
		
		this.addEventListener("onNotify",new ReservationController());
		this.getPage().setAttribute("ACTION","FLUSH");
		Events.sendEvent(new Event("onNotify",this));
		inner.update(this.getPage().getAttribute("COLDATA"));
	}
	
	public void filter(){
		currentFilterOp=((Combobox)this.getFellow("FILTER")).getSelectedIndex();
		inner.refresh();
	}
	
	private void setPara(List data){
		try{
			id.setText(((Listcell)data.get(0)).getLabel());
			name.setText(((Listcell)data.get(1)).getLabel());
			sex.setSelectedIndex(((Listcell)data.get(2)).getLabel().equals("男") ? 0 : 1);
			docType.setSelectedIndex(((Listcell)data.get(3)).getLabel().equals("身份证") ? 0 : 
								 ((Listcell)data.get(3)).getLabel().equals("军人证") ? 1 : 2);
			docNumber.setText(((Listcell)data.get(4)).getLabel());
			tel.setText(((Listcell)data.get(5)).getLabel());
			timeIn.setValue(parseTime(((Listcell)data.get(6)).getLabel()));
			timeOut.setValue(parseTime(((Listcell)data.get(7)).getLabel()));
			type.setSelectedIndex(parseType(((Listcell)data.get(8)).getLabel()));
			roomNr.setText(((Listcell)data.get(9)).getLabel());
			partTime.setChecked(((Listcell)data.get(10)).getLabel().equals("是"));
		}catch(Exception e){}
	}
	
	private Date parseTime(String t){
		int year=new Integer(t.substring(0,4))-1900;
		int month=new Integer(t.substring(5,7))-1;
		int day=new Integer(t.substring(8,10));
		return new Date(year,month,day);
	}
	
	private int parseType(String str){
		if(str.equals("单人间"))
			return 0;
		if(str.equals("双人间"))
			return 1;
		if(str.equals("三人间"))
			return 2;
		if(str.equals("四人间"))
			return 3;
		if(str.equals("套间"))
			return 4;
		if(str.equals("标准间"))
			return 5;
			return 6;
	}
	
	public void timeValidate(){
		Date time1=timeIn.getValue();
		Date time2=timeOut.getValue();
		if(time1 == null)
			throw new WrongValueException(timeIn,"请输入入住时间");
		if(time2 == null)
			throw new WrongValueException(timeOut,"请输入离开时间");
		if(time1.before(new Date()))
			throw new WrongValueException(timeIn,"入住时间不能早于当前");
		if(time2.before(time1))
			throw new WrongValueException(timeOut,"离开时间不能早于入住时间");
		this.getPage().setAttribute("TIMEIN",time1);
		this.getPage().setAttribute("TIMEOUT",time2);
	}
	
	public void typeValidate(){
		int index=type.getSelectedIndex();
		if(index < 0)
			throw new WrongValueException(type,"请输入房间类型");
		this.getPage().setAttribute("ROOMTYPE",type.getSelectedItem().getLabel());
	}
	
	public void roomNrValidate(){
		Integer nr=roomNr.getValue();
		if(nr == null)
			throw new WrongValueException(roomNr,"请输入房间数量");
		if(nr <= 0)
			throw new WrongValueException(roomNr,"房间数量必须大于0");
		this.getPage().setAttribute("ROOMNR",nr);
	}
	
	public void noteValidate(){
		String str=note.getText();
		if(str != null && str.length() > 100)
			throw new WrongValueException(note,"备注长度不能大于100");
		this.getPage().setAttribute("NOTE",str);
	}
	
	public void nameValidate(){
		String str=name.getText();
		if(str == null)
			throw new WrongValueException(name,"请输入客户姓名");
		if(str.length() > 50)
			throw new WrongValueException(name,"客户名字长度不能超过50");
		this.getPage().setAttribute("NAME",str);
	}
	
	public void telValidate(){
		String str=tel.getText();
		if(str != null && str.length() > 15)
			throw new WrongValueException(tel,"客户电话长度不能超过15");
		this.getPage().setAttribute("TEL",tel.getValue());
	}
	
	public void docTypeValidate(){
		if(docType.getSelectedIndex() < 0)
			throw new WrongValueException(docType,"请选择证件类型");
		this.getPage().setAttribute("DOCTYPE",docType.getSelectedItem().getLabel());
	}
	
	public void docNumberValidate(){
		if(docNumber.getText() == null || docNumber.getText().equals(""))
			throw new WrongValueException(docNumber,"请输入证件号码");
		if(docType.getSelectedItem() != null &&
		   docType.getSelectedItem().getLabel().equals("身份证"))
			for(int i=0;i < docNumber.getText().length();i++){
				char ati=docNumber.getText().charAt(i);
				if(ati < '0' || ati > '9')
					throw new WrongValueException(docNumber,"身份号码必须全为数字");
			}
		this.getPage().setAttribute("DOCNUMBER",docNumber.getText());
	}
	
	public void sexValidate(){
		if(sex.getSelectedIndex() < 0)
			throw new WrongValueException(sex,"请选择客人性别");
		this.getPage().setAttribute("SEX",sex.getSelectedItem().getLabel());
	}
	
	public void onAdd(){
		timeValidate();
		typeValidate();
		roomNrValidate();
		noteValidate();
		nameValidate();
		telValidate();
		docTypeValidate();
		docNumberValidate();
		sexValidate();
		this.getPage().setAttribute("PART_TIME",partTime.isChecked());
		this.getPage().setAttribute("ACTION","ADD");
		Events.sendEvent(new Event("onNotify",this));
		inner.update(this.getPage().getAttribute("COLDATA"));
	}
	
	public void onDel(){
		try{
			this.getPage().setAttribute("ID",((Listcell)inner.getColDate(inner.getSelectedIndex()).get(0)).getLabel());
			this.getPage().setAttribute("ACTION","DEL");
			Events.sendEvent(new Event("onNotify",this));
			inner.update(this.getPage().getAttribute("COLDATA"));
		}catch(Exception e){}
	}
}